<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">
        <el-row :gutter="20">
            <el-col :span="10" :offset="3" >
           <el-input placeholder="请输入内容" v-model="input3" class="input-with-select">
                <el-select style="width:130px" v-model="select" slot="prepend" placeholder="请选择">
                    <el-option v-for="item in selectOptions"
                    :key="item.value" 
                    :label="item.lable"
                     :value="item.value"></el-option>
                </el-select>
                <el-button @click="search" slot="append" icon="el-icon-search" >搜索</el-button>
            </el-input>
          </el-col>
        </el-row>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <!-- 具体内容，下拉框 row是下拉框中的对象，包含每个属性的对象id，address-->
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="笔记 ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="发布人">
                <span>{{ props.row.nickname }}</span>
              </el-form-item>
             
              <el-form-item label="笔记标题">
                <span>{{ props.row.title }}</span>
              </el-form-item>

              <el-form-item label="内容">
                <span>{{ props.row.content }}</span>
              </el-form-item>

              <el-form-item label="类型">
                <span>{{ props.row.type }}</span>
              </el-form-item>

              <el-form-item label="发布时间">
                <span>{{ props.row.time }}</span>
              </el-form-item>

              <el-form-item label="评论量">
                <span>{{ props.row.commentNum }}</span>
              </el-form-item>
              <el-form-item label="收藏量">
                <span>{{ props.row.collectNum }}</span>
              </el-form-item>  
              <el-form-item label="点赞量">
                <span>{{ props.row.likeNum }}</span>
              </el-form-item>
               <el-form-item label="分享量">
                <span>{{ props.row.shareNum }}</span>
               </el-form-item>
              <el-form-item label="图片">
                     <el-row :gutter="20">    
                          <el-col v-for="item in props.row.pics" :key="item.id" :span="3">             
                            <img
                            style="width: 100px; height: 100px"
                            :src="item.url"
                            fit="fit"></img>
    
                    </el-col> 
               
                </el-row>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <!-- table head -->
        <el-table-column label="笔记名称" prop="title"> </el-table-column>
        <el-table-column label="发布人" prop="nickname"> </el-table-column>
        <el-table-column label="发布时间" prop="time"> </el-table-column>
        <el-table-column label="类型" prop="type"> </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
          
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="Pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="10"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>

             <!-- 修改提示框 -->
      <el-dialog title="修改笔记信息" v-model="dialogFormVisible">
        <div slot="footer">
          <el-form :model="selectTable">
            <el-form-item label="笔记ID" label-width="100px">
              <el-input
              readonly
                v-model="selectTable.id"
                auto-complete="off"
              ></el-input>
            </el-form-item>
             <el-form-item  label="笔记标题" label-width="100px">
              <el-input type="text"  v-model="selectTable.title"></el-input>
            </el-form-item>
            <el-form-item  label="笔记内容" label-width="100px">
              <el-input type="textarea" autosize v-model="selectTable.content"></el-input>
            </el-form-item>
            
             <el-form-item label="笔记类型" label-width="100px">
              <el-input v-model="selectTable.type"></el-input>
            </el-form-item>
            <el-form-item label="分享数" label-width="100px">
              <el-input type="number" v-model="selectTable.shareNum"></el-input>
            </el-form-item>
            <el-form-item label="点赞量" label-width="100px">
              <el-input type="number" v-model="selectTable.likeNum"></el-input>
            </el-form-item>
             <el-form-item label="评论量" label-width="100px">
              <el-input type="number" v-model="selectTable.commentNum"></el-input>
            </el-form-item>
               <el-form-item label="收藏量" label-width="100px">
              <el-input type="number" v-model="selectTable.collectNum"></el-input>
            </el-form-item>

             <el-form-item label="图片" label-width="100px">
                <el-row  >    
                    <el-col 
                     :span="3" v-for="(item,index) in selectTable.pics" :key="item.id" >             
                            <img ref="img"
                            style="width: 100px; height: 100px"
                            :src="item.url"
                            fit="fit"></img> 
                             <div ref="imgDelete" class="delete-img">
                         <i class="el-icon-delete text-red" @click="deleteImg(item.id,index)"></i>
                  </div>
   
                    </el-col> 
                                  
                </el-row>
                 <el-row type="flex" >    
                    <el-col :span="3">  
                             <el-upload class="avatar-uploader" action="http://localhost:8090/tourism/note/addNotePic" 
                                :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"
                                 :data="{noteId:selectTable.id}"
                                :show-file-list="false"
                            >           
                           <el-button type="primary" >添加图片</el-button>
                             </el-upload>
    
                    </el-col> 
                </el-row>
            </el-form-item>
       
            
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import headTop from "../components/headTop";
import { baseUrl, baseImgPath } from "@/config/env";
import { getNotes, getNotesByLike, getNotesByPage,deleteNotePic,deleteNote } from "@/api/note";

export default {
  data() {
    return {
      baseUrl,
      baseImgPath,
      offset: 0,
      limit: 10,
      count: 0,
      tableData: [],
      currentPage: 1,
      selectTable: {},
      dialogFormVisible: false,
      addr: {},
      notes: {},
      input3: "",
      select: 1,
      selectOptions: [
        {
          lable: "笔记标题",
          value: "1",
        },
      ],
    };
  },
  created() {
    this.select = this.selectOptions[0].value;
    this.initData();
    console.log(11111);
  },
  components: {
    headTop,
  },
  //这里运用async/await 进行异步处理，保证BMap加载进来后才执行后面的操作
  async mounted() {},

  methods: {
     handleAvatarSuccess(res, file) {
      if(res.code>=0){
        let pics = this.selectTable.pics
        this.selectTable.pics = pics.concat(res.data)       
        this.$message({
          message: '添加成功',
          type:"success"
        });
      }else{
         this.$message({
          message: res.message,
          type:"fail"
        });
      }
        
        console.log(res);
      },
      beforeAvatarUpload(file) {
        
        const isJPG = file.type === 'image/jpeg'||file.type=='image/jpg'||file.type=='png';
        const isLt2M = file.size / 1024 / 1024 < 1;
 
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG ,PNG,JPEG格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 1MB!');
        }
        return isJPG && isLt2M;
      },
     
    // 删除图片
    async deleteImg(id,index) {
      this.$confirm("是否删除该照片?", "删除", {
         distinguishCancelAndClose: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async() => {
          console.log("picture Id",id);
          if(id){
           await  deleteNotePic(id)
            this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.selectTable.pics.splice(index,1)
          }
         
        })
        .catch((action) => {
          if (action === 'cancel'){
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          }
        });
    },


     handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      }, 
    async search() {
      //内容，类型
      await this.getList();
    },
    //初始化数据
    async initData() {
      try {
        this.getList();

        // const countData = await getResturantsCount();

        // this.getResturants();
      } catch (err) {
        console.log("获取数据失败", err);
      }
    },
    async getList() {
      let info = this.input3;
      let currentPage = this.currentPage;
      let pageSize = this.limit;
      let res;
      if (info == "")
        res = await getNotesByPage({
          currentPage,
          pageSize,
          sortType: 1,
          info,
        });
      else res = await getNotesByLike({ currentPage, pageSize, info });

      if (res && res.code >= 0) {
        this.tableData = res.data.rows;
        this.count = res.data.totalCount;
      } else {
        console.log(res.message);
      }
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.offset = (val - 1) * this.limit;
      this.getList();
    },
    async handleEdit(index, row) {
      //   this.addr = ;
      this.selectTable = row
      this.dialogFormVisible = true;
      
    },
    async handleDelete(index, row) {
      try {
        const res = await deleteNote(row.id);
        if (res.code >= 0) {
          this.$message({
            type: "success",
            message: "删除成功",
          });
          this.tableData.splice(index, 1);
          this.count--;
        } else {
          throw new Error(res.message);
        }
      } catch (err) {
        this.$message({
          type: "error",
          message: err.message,
        });
       
      }
    },

    //更新景点
    async update() {
      try {
        //把address添加到selectable中
        // Object.assign(this.selectTable, this.addr);

        console.log("this.selectTable", this.selectTable);
        if (this.addr.id) {
          this.selectTable.addr = this.addr;
        }
        const res = await updateSceneryById(this.selectTable);
        if (res.code >= 0) {
          this.$message({
            type: "success",
            message: "更新景点信息成功",
          });

          this.getList();
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      } catch (err) {
        console.log("更新餐馆信息失败", err);
      }
      this.dialogFormVisible = false;
    },
  },
};
</script>

<style lang="less">
@import "../style/mixin";
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.table_container {
  padding: 20px;
}
.Pagination {
  display: flex;
  justify-content: flex-start;
  margin-top: 8px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>
